package net.aiform.record.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.aiform.bean.JSONMessage;
import net.aiform.record.bean.RecordBean;
import net.aiform.record.service.RecordService;
import net.aiform.template.pojo.Template;
import net.aiform.template.service.TemplateService;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public final class ListRecordAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private TemplateService templateService;
	private RecordService recordService;
	private HttpServletRequest req;
	private JSONMessage json;
	private Template template;
	private String code;
	private String arg1;

	public String fetchRecords() {
		String code = this.req.getServletPath();
		code = code.substring(code.lastIndexOf("/") + 1);
		Template t = this.templateService.get(code);
		List<RecordBean> rs = this.recordService.getRecordBean(t);
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("template", t);
		m.put("records", rs);
		json.setData(m);
		return Action.SUCCESS;
	}

	public String execute() {
		return Action.SUCCESS;
	}

	public ListRecordAction() {
		super();
		this.json = new JSONMessage();
	}

	public ListRecordAction(TemplateService templateService, RecordService recordService) {
		super();
		this.templateService = templateService;
		this.recordService = recordService;
		this.json = new JSONMessage();
	}

	public void prepare() throws Exception {

	}

	@JSON(serialize = false)
	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	@JSON(serialize = false)
	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	@JSON(serialize = false)
	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	@JSON(serialize = false)
	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	@JSON(serialize = false)
	public HttpServletRequest getReq() {
		return req;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@JSON(serialize = false)
	public RecordService getRecordService() {
		return recordService;
	}

	public void setRecordService(RecordService recordService) {
		this.recordService = recordService;
	}

	public JSONMessage getJson() {
		return json;
	}

	public void setJson(JSONMessage json) {
		this.json = json;
	}
}
